package client;
import gui.Cart_Gui;
import gui.Complaint_Gui;
import gui.Delivery_Greeting_Gui;
import gui.FileComplaint_GUI;
import gui.GenerateReports_GUI;
import gui.LogIn_GUI;
import gui.MemberAccountGUI;
import gui.MemberApprovalGUI;
import gui.MyComplaints_GUI;
import gui.Order_GUI;
import gui.Search_GUI;
import gui.SubscribeGUI;
import gui.Survey_GUI;
import gui.SystemSettings_GUI;
import gui.Payment_GUI;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.TableCellRenderer;

import server.SendToClient;

public class MainScreen extends JFrame implements ActionListener 
{
	ComplaintThread cp;
	public JFrame frame = null;  
	public setImage img= null;
	public JButton logOut = null;
	public JButton viewCatalog  = null;
	public JButton createMemberAccount  = null;
	public JButton Orders   = null;
	public JButton subscription  = null;
	public JButton cart  = null;
	public JButton analyzeSurvey  = null;
	public JButton complaintHandling  = null;
	public JButton fileComplaint  = null;
	public JButton myComplaints = null;
	public JButton surveyManaging  = null;
	public JButton catalogManaging  = null;
	public JButton makeReports  = null;
	public JButton SystemSettings  = null;
	public JButton search  = null;
	public JButton displayReports=null;
	public JButton dispBalance;
	public Statement stmt= null;  
	public Connection conn= null; 
	public MemberAccountGUI mag= null;
	public MemberApprovalGUI mapg=null;
	public SubscribeGUI sg= null;
	public Complaint_Gui cg= null;
	public Survey_GUI survey_gui = null;
	public Search_Control vCatalog = null;
	public static Search_GUI src = null;
	public Order_GUI order = null;
	public Order order_control = null;
	public FileComplaint_GUI fc = null;
	public SystemSettings_GUI ss = null;
	public static Integer read = -1;
	public Payment_GUI payment_gui = null;
	public CatalogManaging catalogManag = null;
	public String whatToShow = "allItems"; 
	public String callFrom = "MainScreen";
	public String orderCallFrom = "MainScreen";
	public GenerateReports_GUI grg=null;
	public MyComplaints_GUI mc=null;
	public Payment_GUI pay = null;
	
	
	public MainScreen(int width, int height )
	{
		img = new setImage("MainScreen");
		img.setLayout(null);
		this.setPreferredSize(new Dimension(width, height));
		this.setBounds((int)Main.dim.getWidth()/2-(width/2), (int)Main.dim.getHeight()/2-(height/2), width, height);
		this.setResizable(false);
		this.setTitle("Main Screen");
		
		logOut = new JButton("LogOut");
		logOut.setBounds(270,70,110,30);
		logOut.addActionListener(this);
		img.add(logOut);
		
		viewCatalog= new JButton("View Catalog");
		viewCatalog.setBounds(10,70,110,30);
		viewCatalog.addActionListener(this);
		img.add(viewCatalog);
		
		search= new JButton("Search");
		search.setBounds(50,130,300,40);
		search.addActionListener(this);
		img.add(search);
		
		if(LogIn.usr.getPriv() == 6) //non member
		{
			createMemberAccount	= new JButton("Create Member Account");
			createMemberAccount.setBounds(50,180,300,40);
			createMemberAccount.addActionListener(this);
			img.add(createMemberAccount);
			
			myComplaints = new JButton("Complaints");
			myComplaints.setBounds(140,70,110,30);
			myComplaints.addActionListener(this);
			img.add(myComplaints);
		}
		
		if(LogIn.usr.getPriv() <= 5) //member +
		{
			Orders	= new JButton("Place / Handle Order");
			Orders.setBounds(50,180,300,40);
			Orders.addActionListener(this);
			img.add(Orders);
			
			subscription = new JButton("Create Subscription");
			subscription.setBounds(50,230,300,40);
			subscription.addActionListener(this);
			img.add(subscription);
			
			cart = new JButton("Go To Cart");
			cart.setBounds(50,280,300,40);
			cart.addActionListener(this);
			img.add(cart);
			
			if(LogIn.usr.getPriv() == 5)
			{
				myComplaints = new JButton("Complaints");
				myComplaints.setBounds(140,70,110,30);
				myComplaints.addActionListener(this);
								
				dispBalance=new JButton("Display Balance");
				dispBalance.setBounds(50, 330, 300, 40);
				dispBalance.addActionListener(this);
				
				img.add(myComplaints);
				img.add(dispBalance);
			}
			
			if(LogIn.usr.getPriv() == 4)//expert
			{
				analyzeSurvey = new JButton("Analyze Survey");
				analyzeSurvey.setBounds(50,180,300,40);
				analyzeSurvey.addActionListener(this);
				img.add(analyzeSurvey);
			}
			
			if(LogIn.usr.getPriv() == 3)//client support employee
			{
				complaintHandling = new JButton("Complaint Handling");
				complaintHandling.setBounds(50,180,300,40);
				complaintHandling.addActionListener(this);
				img.add(complaintHandling);
				
				surveyManaging = new JButton("Survey Managing");
				surveyManaging.setBounds(50,230,300,40);
				surveyManaging.addActionListener(this);
				img.add(surveyManaging);
				
				fileComplaint= new JButton("File Complaint");
				fileComplaint.setBounds(50,280,300,40);
				fileComplaint.addActionListener(this);
				img.add(fileComplaint);
				
				cp=new ComplaintThread();
				cp.start();
			}
			
			if(LogIn.usr.getPriv() == 2)//employee
			{
				surveyManaging = new JButton("Survey Managing");
				surveyManaging.setBounds(50,230,300,40);
				surveyManaging.addActionListener(this);
				img.add(surveyManaging);
				
				
				catalogManaging = new JButton("Catalog Managing");
				catalogManaging.setBounds(50,180,300,40);
				catalogManaging.addActionListener(this);
				img.add(catalogManaging);
			} 
			
			if(LogIn.usr.getPriv() == 1)//store manager
			{
				makeReports = new JButton("Generate Reports");
				makeReports.setBounds(50,180,300,40);
				makeReports.addActionListener(this);
				img.add(makeReports);
			}
			
			if(LogIn.usr.getPriv() == 0)//net manager
			{
				SystemSettings = new JButton("System Settings");
				SystemSettings.setBounds(50,180,300,40);
				SystemSettings.addActionListener(this);
				
				displayReports=new JButton("Display Reports");
				displayReports.setBounds(50, 230, 300, 40);
				displayReports.addActionListener(this);
				
				img.add(SystemSettings);
				img.add(displayReports);
			}			
			
		}
		
		if(LogIn.usr.getPriv() < 5)
		{
			img.remove(cart);
			img.remove(subscription);
			img.remove(Orders);
	//		img.remove(fileComplaint);

			viewCatalog.setBounds(10,70,180,30);
			logOut.setBounds(210,70,176,30);
		}
				
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE );
		this.add(img);
		this.pack();
		showMainScreen();
		if(LogIn.usr.getPriv() == 2)
		{
			Survey.checkRead();
			if(read!=0)	JOptionPane.showMessageDialog(null,"You Have Surveys Waiting For You!!!!","New Surveys",JOptionPane.DEFAULT_OPTION);
		}
	}
	
	public void hideMainScreen()
	{
		this.setEnabled(false);
		this.setVisible(false);
	}
	
	public void showMainScreen()
	{
		this.setEnabled(true);
		this.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) 
	{	
		if(e.getSource() == logOut)
		{	
			//cp.stop();
			Main.clie = LogIn_GUI.conn;
			
			LogIn.usr.logout = true;
			Main.clie.accept(LogIn.usr);
			
			Main.clie.flag = false;
			while(Main.clie.flag == false);
			Main.clie.flag = false;
			
			hideMainScreen();
			Main.log.setVisible(true);
			Main.log.setEnabled(true);
			
		}
		
		if(e.getSource() == catalogManaging)
		{
			hideMainScreen();
			catalogManag = new CatalogManaging(whatToShow);
		}
		
		if(e.getSource() == Orders)
		{
			hideMainScreen();
			if(order != null)	
				order.dispose();
			order = new Order_GUI(orderCallFrom);
		}
		
		if(e.getSource() == viewCatalog)
		{
			vCatalog = new Search_Control("-1", "-1", "-1", Search_GUI.colors, Search_GUI.types, "catalog");
			this.setVisible(false);
			
			if(Main.catalog != null)
				Main.catalog.dispose();
			Main.catalog = new Catalog("MainScreen");
		}
		
		if(e.getSource() == cart)
		{
			this.setEnabled(false);
			if(Main.cart != null)	
    			Main.cart.dispose();
			Main.cart = new Cart_Gui("MainScreen");
		}
		
		if(e.getSource() == search)
		{
			hideMainScreen();
			if(src != null)	
				src.dispose();
			src = new Search_GUI(callFrom);
		}
		
		if(e.getSource()==createMemberAccount)
		{
			hideMainScreen();
			mag = new MemberAccountGUI();
			mag.btnBack.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					mag.dispose();
					showMainScreen();
				}
			});
		}
		
		if(e.getSource()==analyzeSurvey)
		{
			hideMainScreen();
			survey_gui = new Survey_GUI(1);
		}
		
		if(e.getSource()==surveyManaging)
		{
			hideMainScreen();
			if(LogIn.usr.getPriv() == 2)
				survey_gui = new Survey_GUI(2);//Employee panel
			else if(LogIn.usr.getPriv() == 3)survey_gui = new Survey_GUI(3);//Service panel
		}
		
		if(e.getSource()==complaintHandling)
		{
			hideMainScreen();
			cg = new Complaint_Gui();
			cg.btnBack.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					cg.dispose();
					showMainScreen();
				}
			});
		}
		
		if(e.getSource()==subscription)
		{
			hideMainScreen();
			sg=new SubscribeGUI();
			sg.btnBack.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					sg.dispose();
					showMainScreen();
				}
			});
		}
		
		if(e.getSource()==fileComplaint)
		{
			hideMainScreen();
			fc = new FileComplaint_GUI();
			fc.btnBack.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					fc.dispose();
					showMainScreen();
				}
			});
		}
		
		if(e.getSource()==SystemSettings)
		{
			hideMainScreen();
			ss = new SystemSettings_GUI();
			ss.btnBack.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					ss.dispose();
					showMainScreen();
				}
			});
		}
		
		if(e.getSource()==makeReports)
		{
			hideMainScreen();
				grg = new GenerateReports_GUI();
				grg.btnBack.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						grg.dispose();
						showMainScreen();
					}
				});
			
		}
		
		if(e.getSource() == myComplaints)
		{
			hideMainScreen();
			mc = new MyComplaints_GUI();
			mc.btnBack2.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					mc.dispose();
					showMainScreen();
				}
			});
		}
		
		if(e.getSource()==displayReports)
		{
			hideMainScreen();
				grg = new GenerateReports_GUI();
				grg.btnBack.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent e) {
						grg.dispose();
						showMainScreen();
					}
				});
			
		}
		if(e.getSource()==dispBalance)
		{
			String[] s={"checkSub",String.valueOf(LogIn.usr.getID())};
			ObjHandle.sendToServer(s);
			int answer=(Integer)Main.clie.getResult();
			JOptionPane.showMessageDialog(null, "Balance: "+answer);
		}
	}
}
